package com.daafan.minilib.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.daafan.minilib.R;
import com.daafan.minilib.data.Skin;

public class SkinActivity extends BaseActivity {

	ListView		listView_skinList;
	SkinListAdapter	mAdapter;
	List<String>	skinUrls	= new ArrayList<String>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_skin);

		initTopBar("皮肤", R.drawable.back, 0);

		skins();

		listView_skinList = (ListView) findViewById(R.id.listView_skin);

		mAdapter = new SkinListAdapter();
		listView_skinList.setAdapter(mAdapter);
	}

	private void skins() {
		skinUrls.add("drawable://" + R.drawable.h_bg);
		skinUrls.add("drawable://" + R.drawable.skin1);
		skinUrls.add("drawable://" + R.drawable.skin2);
		skinUrls.add("drawable://" + R.drawable.skin3);
		skinUrls.add("drawable://" + R.drawable.skin4);
		skinUrls.add("drawable://" + R.drawable.skin5);
	}

	@Override
	protected void onTopLeftClick() {
		back();
	}

	class SkinListAdapter extends BaseAdapter {

		ViewHolder	holder;

		class ViewHolder {
			ImageView	img_skin0;
			ImageView	img_skin1;
		}

		@Override
		public int getCount() {
			int size = skinUrls.size() / 2 + skinUrls.size() % 2;
			return size;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = getLayoutInflater().inflate(R.layout.skin_item, null);

				holder = new ViewHolder();

				holder.img_skin0 = (ImageView) convertView.findViewById(R.id.img_skin0);
				holder.img_skin1 = (ImageView) convertView.findViewById(R.id.img_skin1);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			// holder.img_skin0.setLayoutParams(new LinearLayout.LayoutParams((int) (screenWidth / 2),
			// (int) ((screenWidth / 2) * (1920 / 1080))));
			// holder.img_skin1.setLayoutParams(new LinearLayout.LayoutParams((int) (screenWidth / 2),
			// (int) ((screenWidth / 2) * (1920 / 1080))));

			mImageLoader.displayImage(skinUrls.get(position * 2), holder.img_skin0);
			holder.img_skin0.setOnClickListener(new SkinImageOnClickListener(skinUrls.get(position * 2)));

			int index = position * 2 + 1;
			if (index < skinUrls.size()) {
				mImageLoader.displayImage(skinUrls.get(index), holder.img_skin1);

				holder.img_skin1.setOnClickListener(new SkinImageOnClickListener(skinUrls.get(index)));
			}

			return convertView;
		}
	}

	class SkinImageOnClickListener implements OnClickListener {

		String	url;

		public SkinImageOnClickListener(String url) {
			this.url = url;
		}

		@Override
		public void onClick(View v) {
			Skin skin = new Skin();
			skin.setUrl(url);
			Skin.deleteAll(mContext);

			db.save(skin);

			back();
		}
	}
}
